# -*- coding: utf-8 -*-
# @Time    : 2022/3/18 15:16

from Common.mysql import db
from Common.public_method import utc_8
from src.ApiTest.ConfigurationData.Database.data_database import TestData, TestDataSchema
from sqlalchemy.exc import SQLAlchemyError
from Common.yaml_method import YamlMethod


class UpdateData:
    """
    更新测试数据信息接口
    """

    @staticmethod
    def update_data(project_id, project_name, data_id, data_name, data_type, key, value, update_user):
        """
        更新测试数据信息接口
        :param project_id: 项目ID
        :param project_name: 项目名称
        :param data_id: 测试数据ID
        :param data_name: 测试数据名称
        :param data_type: 测试数据类型
        :param key:
        :param value:
        :param update_user: 更新人
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        update_time = utc_8()

        data = TestData.query.filter_by(id=data_id).first()

        if data:
            data.projectId = project_id
            data.projectName = project_name
            data.data_name = data_name
            data.data_type = data_type
            data.data_key = key
            data.data_value = value
            data.update_time = update_time
            data.update_user = update_user

            try:
                db.session.commit()
            except SQLAlchemyError:
                db.session.rollback()
                res = {
                    'code': code[6],
                    'data': [],
                    'message': '测试数据信息更新失败'
                }
                return res
            res = {
                'code': code[0],
                'data': [],
                'message': '测试数据信息更新成功'
            }
            return res
        else:
            res = {
                'code': code[3],
                'data': [],
                'message': '测试数据不存在'
            }
            return res
